<template>
  <view class="content">
    <custom-navi :show-home="false">详情编辑</custom-navi>
    <edit
      paddingBottom="20"
      ref="edit"
      placeText="写点什么吧..."
      @submit="handleSubmit"
    ></edit>
  </view>
</template>

<script>
import edit from '../../components/wjx-edit/wjx-edit.vue';
import { submitStoreDetail, getStoreInformation } from '../../../../api'
export default {
  data () {
    return {
      title: 'Hello',
      shopInfo: {}
    }
  },
  components: {
    edit
  },

  onLoad () {

    let _self = this
    uni.getStorage({
      key: 'shopInfo',
      success: function (res) {

        _self.shopInfo = res.data



      }
    });
  },
  watch: {
    shopInfo: {
      handler (val, oval) {
        console.log(val)
        let editData = this.$refs.edit.submit();
        editData.editItems = JSON.parse(this.shopInfo.storeDetails)
        // console.log(_self.shopInfo.storeDetails)
      }
    }
  },
  methods: {
    async handleSave (storeDetails) {
      const params = {
        id: this.shopInfo.id,
        storeDetails: storeDetails
      }
      try {
        const { data } = await submitStoreDetail(params)
        const { data: shopInfo } = await getStoreInformation()
        uni.setStorageSync('shopInfo', shopInfo);
        return this.$utils.common.toast.fail("保存成功");

      } catch (err) {
        if (err.code) {
          if (err.code !== 200) {
            return this.$utils.common.toast.fail(err.data.message);
          }
          return this.$utils.common.toast.fail(err.errMsg);
        }
      }
    },
    handleSubmit () {
      //子组件的data
      let editData = this.$refs.edit.submit();
      const params = encodeURIComponent(JSON.stringify(editData.editItems))
      this.handleSave(params)
    }
  }
}
</script>

<style>
</style>
